package com.qc.serviceedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qc.serviceedu.client.VodClient;
import com.qc.serviceedu.entity.EduVideo;
import com.qc.serviceedu.mapper.EduVideoMapper;
import com.qc.serviceedu.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author AltmanQc
 * @since 2020-12-25
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    @Autowired
    private VodClient vodClient;

    //1.根据课程id删除小节
    //删除小节删除视频的对应文件
    @Override
    public void removeVideoByCourseId(String courseId) {
        //1.根据课程id查询所有课程id
        QueryWrapper<EduVideo> videoQueryWrapper = new QueryWrapper<>();
        videoQueryWrapper.eq("course_id",courseId);
        videoQueryWrapper.select("video_source_id");
        List<EduVideo> videoList = baseMapper.selectList(videoQueryWrapper);
        for (EduVideo eduVideo : videoList) {
            String sourceId = eduVideo.getVideoSourceId();
            if (!StringUtils.isEmpty(sourceId)){
                vodClient.removeVideo(sourceId);
            }
        }
        QueryWrapper<EduVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        baseMapper.delete(queryWrapper);
    }
}
